package com.xinxin.base.util;

import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;

/**
 * @author ys
 * @date 2020/5/15
 */
public class UserAgentUtil {

    private UserAgentUtil() {
    }

    public static String getUserOS(UserAgent userAgent) {
        String os = null;

        if (userAgent.mac.match) {
            os = userAgent.getBrowserVersion(userAgent.mac);
        } else if (userAgent.linux.match) {
            os = "Linux";
        } else if (userAgent.win.match) {
            os = "Windows ";
            userAgent.win.index += userAgent.win.mark.length();
            String version = userAgent.getBrowserVersion(userAgent.win);
            switch (version.trim()) {
                case "5.0":
                    os += "2000";
                    break;
                case "5.1":
                    os += "XP";
                    break;
                case "5.2":
                    os += "2003";
                    break;
                case "6.0":
                    os += "Vista";
                    break;
                case "6.1":
                    os += "7";
                    break;
                case "6.2":
                    os += "8";
                    break;
                case "6.3":
                    os += "8.1";
                    break;
                case "10":
                    os += "10";
                    break;
            }
        }

        return os;
    }

    public static String getUserBrowser(UserAgent userAgent) {
        String browser = null;

        boolean containIE = userAgent.ie.match || userAgent.ie11.match && userAgent.win.match;

        if (userAgent.sogou.match) {
            if (containIE) {
                browser = "搜狗" + userAgent.getBrowserVersion(userAgent.ie);
            } else if (userAgent.chrome.match) {
                browser = "搜狗" + userAgent.getBrowserVersion(userAgent.chrome);
            }
        } else if (userAgent.chrome.match) {
            browser = userAgent.getBrowserVersion(userAgent.chrome);
        } else if (userAgent.safari.match) {
            userAgent.safari.index = userAgent.userAgent.indexOf("Version");
            browser = "Safari " + userAgent.getBrowserVersion(userAgent.safari);
        } else if (userAgent.firefox.match) {
            browser = userAgent.getBrowserVersion(userAgent.firefox);
        } else if (containIE) {
            if (userAgent.ie.match) {
                browser = userAgent.getBrowserVersion(userAgent.ie);
            } else {
                browser = "IE 11";
            }
        }

        return StringUtils.replace(browser, "/", StringUtils.SPACE);
    }

    public static class UserAgent {

        @RequiredArgsConstructor
        private static class Browser {

            @NonNull
            private String mark;
            @NonNull
            private String versionMark;
            private int index = -1;
            private boolean match = false;

            private void match(String userAgent) {
                index = userAgent.indexOf(mark);
                match = index > 0;
            }
        }

        private Browser ie = new Browser("MSIE", "IE x.x");
        private Browser ie11 = new Browser("rv:", StringUtils.EMPTY);
        private Browser firefox = new Browser("Firefox", "Firefox/xx");
        private Browser sogou = new Browser("MetaSr", StringUtils.EMPTY);
        private Browser chrome = new Browser("Chrome", "Chrome/xx");
        private Browser safari = new Browser("Safari", "Version/x.x.x.x");

        private Browser mac = new Browser("Mac OS X", "MacOS X xxxxxxxx");
        private Browser win = new Browser("Windows NT", "x.x");
        private Browser linux = new Browser("Linux", StringUtils.EMPTY);

        private StringBuilder userAgent;

        public UserAgent(String userAgent) {
            this.userAgent = new StringBuilder("[" + userAgent + "]");
            userAgent = this.userAgent.toString();

            ie.match(userAgent);
            ie11.match(userAgent);
            firefox.match(userAgent);
            sogou.match(userAgent);
            chrome.match(userAgent);
            safari.match(userAgent);

            mac.match(userAgent);
            win.match(userAgent);
            linux.match(userAgent);
        }

        private String getBrowserVersion(Browser browser) {
            return userAgent.substring(browser.index, browser.index + browser.versionMark.length());
        }
    }
}
